package com.hui.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hui.config.FindFile;
import com.hui.entity.Book;
import com.hui.entity.User;
import com.hui.mapper.BookMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.File;
import java.util.List;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hui.service
 * @Author: HUI
 * @CreateTime: 2021-12-05 22:25
 * @Description:
 */

@Service
@Slf4j
public class BookService {

    @Autowired
    BookMapper bookMapper;


    //分页模糊查询
    public Page<Book> queryBook(Integer pageNum,
                                Integer pageSize,
                                String searchStr) {

        Page<Book> bookPage = new Page<>(pageNum, pageSize);


        //lambda 模糊查询：user 表中名字相似于 search 的

        LambdaQueryWrapper<Book> wrapper = Wrappers.<Book>lambdaQuery();
        if (!searchStr.isBlank()) {
            wrapper.like(Book::getBookName, searchStr);
        }


        return bookMapper.selectPage(bookPage, wrapper);


    }

    //新增书籍
    public int saveUser(Book book) {
        LambdaQueryWrapper<Book> wrapper = Wrappers.<Book>lambdaQuery().eq(Book::getBookName, book.getBookName());
        Book books = bookMapper.selectOne(wrapper);

        return books == null ? bookMapper.insert(book) : -1;
      /*  if (books!=null){
            return -1;
        }else {
            return bookMapper.insert(book);
        }*/

    }


    //修改书籍信息
    public int updateBook(@RequestBody Book book) {
        return bookMapper.updateById(book);
    }

    //删除下架
    public int deleteBook(Integer id) {
        Book book = bookMapper.selectById(id);
        BookService.sealFindCover(book);
        return bookMapper.deleteById(book);

    }

    //批量删除
    public int bulkDeletion(List<Integer> ids) {
        BookService service = new BookService();
        int sum = 0;
        if (ids.size() > 0) {
            try {
                ids.forEach(e -> {
                    Book book = bookMapper.selectById(e);
                    BookService.sealFindCover(book);
                    bookMapper.deleteById(book);

                });
//                bookMapper.deleteBatchIds(ids) 按照 ids集合删除多个
            } catch (NullPointerException e) {
                System.out.println("空指针异常了！");
                return -1;
            }

            return 1;
        }

        return -1;
    }


    //封装查询是否有图片，有就删除
    public static void sealFindCover(Book book) {
        if (book.getCover() != null) {
            String filename = book.getCover();
            String replace = filename.replace("http://localhost:8888/files/download/", "");
            String file = FindFile.findFile(replace);
            if (!file.isBlank())
            new File(file).delete();

        }
    }

}
